package de.bht.fb6.mobisand.avoidlist.activities;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds;
import android.provider.ContactsContract.Contacts;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import de.bht.fb6.mobisand.avoidlist.R;
import de.bht.fb6.mobisand.avoidlist.model.ToAvoid;

/**
 * Activity die die Ansicht für ein einzelnes ToAvoid verwaltet.
 * 
 * @author Mario Dörwald, Bruno Kirschner
 * @since 08. Jan 2013
 */
public class SingleToAvoidActivity extends Activity {
	
	/**
	 * Diese Konstante wird von der Kontaktauswahl Acitivity
	 * als Ergebnis geliefert wenn keine Probleme aufgetreten sind.
	 */
	private final static int CONTACT_RESULT = 100;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.toavoid_single);
		
		/** Aus den Extras des Intents wird der ??bergebene ItemIndex abgerufen... **/
		int itemIndex = this.getIntent().getExtras().getInt("itemIndex");
		/** ... und damit aus der ListActivity das entsprechende Item abgerufen **/
		ToAvoid singleItem = ListToAvoidActivity.getToAvoidList().get(itemIndex);
		
		/** Das Textfeld R.id.toAvoidDisplay bekommt unser Item als Text **/
		final EditText avoid = (EditText) this.findViewById(R.id.toAvoidDisplay);
		avoid.setText(singleItem.toString());
	}
	
	public void doLaunchContactPicker(View view) {  
	    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,  
	            Contacts.CONTENT_URI);  
	    startActivityForResult(contactPickerIntent, CONTACT_RESULT);  
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
	    if (resultCode == RESULT_OK) {  
	        switch (requestCode) {  
	        case CONTACT_RESULT:  
	            // handle contact results
	        	for (String s : data.getExtras().keySet()) {
	        		Log.i(this.getClass().getName(),s+": "+data.getExtras().get(s).toString());
	        	}
	        	
	        	Log.i(this.getClass().getName(), data.getData().toString());
	        	Log.i(this.getClass().getName(), data.getData().getLastPathSegment());
	        	
	        	Uri contactUri = data.getData();
	        	String contactID = "";
	        	String contactNumber = "";
	        	
	        	Cursor cursorID = this.getContentResolver().query(contactUri, new String[] { Contacts._ID }, null, null, null);
	        	if (cursorID.moveToFirst()) {
	        		contactID = cursorID.getString(cursorID.getColumnIndex(Contacts._ID));
	        	}
	        	cursorID.close();
	        	
	        	Cursor cursorPhone = this.getContentResolver().query(CommonDataKinds.Phone.CONTENT_URI, new String[] {CommonDataKinds.Phone.NUMBER}, CommonDataKinds.Phone.CONTACT_ID + " = ? ", new String[] {contactID}, null);
	        	if (cursorPhone.moveToFirst()) {
	        		contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(CommonDataKinds.Phone.NUMBER));
	        	}
	        	cursorPhone.close();
	        	
	        	Log.i(this.getClass().getName(), contactNumber);
	            break;
	        }  
	    } else {  
	        // gracefully handle failure  
	    }  
	}  
}
